package net.zjitc.android2019.ch3storage;

import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import net.zjitc.android2019.R;
import java.util.HashMap;

public class Ch3Qq extends AppCompatActivity {
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    Button btn,btn2;
    EditText ed_name,ed_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch3_qq);
        btn=findViewById(R.id.btn_ch3_work_denglu);
        btn2=findViewById(R.id.btn_ch3_work_remeber);
        ed_name=findViewById(R.id.ed_number_qq);
        ed_password=findViewById(R.id.ed_password_qq);


        HashMap<String,String> hashMap = FileSPSaveQQ.get(Ch3Qq.this);
        ed_name.setText(hashMap.get("name"));
        ed_password.setText(hashMap.get("pwd"));
        /*从sp文件中获取账号密码并显示在输入框*/

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*1.若输入框为空，则Toast提示不能空
                * 2.若不为空，将账号密码保存：sp file*/
                String name = ed_name.getText().toString().trim();
                String pwd  = ed_password.getText().toString();
                if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(Ch3Qq.this,"账号密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    /*调用SPSaveQQ中的方法啊完成：将账号密码保存sp*/
                    boolean flag=FileSPSaveQQ.sava(Ch3Qq.this,name,pwd);
                    if (flag){
                        Toast.makeText(Ch3Qq.this,"保存成功",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(Ch3Qq.this,"保存失败",Toast.LENGTH_SHORT).show();
                    }
                    }
            }
        };
        btn.setOnClickListener(listener);
        String[] p={"android.permission.WRITE_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(Ch3Qq.this,p,1);
    }


}
